package com.byx.chatclient.im;

import android.util.Log;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.entity.message.AudioMsgBody;
import com.byx.chatclient.entity.message.Message;
import com.byx.chatclient.event.EventType;
import com.byx.chatclient.event.MessageEvent;
import com.byx.imclientutil.entity.BaseMessage;
import com.byx.imclientutil.entity.MessageContentType;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * GroupChatMessageHandler
 *
 * @author Superb
 * @date 2021/4/5 13:13
 */
public class GroupChatMessageHandler implements IMessageHandler {

    private static final String TAG = GroupChatMessageHandler.class.getSimpleName();

    @Override
    public void execute(BaseMessage message) {
        Log.d(TAG, "收到单聊消息，message=" + message);

        Message msg = MessageConverter.toMessage(message);
        switch (message.getContentType()){
            case MessageContentType.AUDIO:
                AudioMsgBody body = (AudioMsgBody) msg.getBody();
                if (body.getLocalPath() == null){
                    body.setLocalPath("");
                }
                File audioFile = new File(body.getLocalPath());
                if (body.getLocalPath().isEmpty() || !audioFile.exists()){
                    String remoteUrl = body.getRemoteUrl();
                    String fileName = remoteUrl.substring(remoteUrl.lastIndexOf('/') + 1);
//                    UploadFilesUtil.download(mFile,remoteUrl);
                    body.setLocalPath(ChatApp.getInstance().getFilesDir()+ "/"+fileName);
                }

                break;
            default:break;
        }
        /*
         * 将消息封装到事件并分发下去
         */
        MessageEvent event = MessageEvent.getInstance(EventType.GROUP_CHAT_MSG);
        event.setData(msg);
        EventBus.getDefault().post(event);
    }
}
